﻿using System.Windows.Controls;
using System.Windows;
using Ops.Mpc.Presenters;

namespace Ops.Mpc.States
{
    public class StateTemplate : DataTemplate, IStateTemplate
    {
        #region Fields & Properties
        public string FocusControlName
        {
            get;
            set;
        }

        public Style Style
        {
            get;
            set;
        }
        #endregion

        #region Constructors
        public StateTemplate()
        {

        }

        #endregion

        #region Methods
        public void Execute(Presenter presenter, IState oldState, IState newState)
        {
            presenter.SetStateTemplate(this, oldState, newState);
            FocusHelper.Focus(presenter, this, this.FocusControlName);


        }

        static class FocusHelper
        {
            public static void Focus(Presenter presenter, StateTemplate contentDefinition, string focusControlName)
            {
                presenter.Dispatcher.BeginInvoke(
                    delegate()
                    {
                        if (!string.IsNullOrEmpty(contentDefinition.FocusControlName))
                        {
                            Control control = presenter.GetCurrentStateTemplateChild(contentDefinition.FocusControlName) as Control;

                            if (control != null)
                                control.Focus();
                        }
                    }
               );
            }
           
        }

        #endregion

    }
}
